package com.tian.mybatis.config;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author tianwc  公众号：java后端技术全栈、面试专栏
 * @version 1.0.0
 * @date 2023年09月21日 11:51
 * 在线刷题 1200+题和1000+篇干货文章：<a href="https://woaijava.cc/">博客地址</a>
 * <p>
 * 使用dom4j解析XxxMapper.xml文件
 */
public class XmlMapperBuilder {
    private Configuration configuration;

    public XmlMapperBuilder(Configuration configuration) {
        this.configuration = configuration;
    }

    public void parse(InputStream inputStream) throws DocumentException {
        SAXReader saxReader = new SAXReader();
        //拿到xml文档
        Document document = saxReader.read(inputStream);
        //-------解析xml文档

        //获取xml文件根节点
        Element rootElement = document.getRootElement();

        /**
         * <mapper namespace="com.tian.test.UserMapper">
         * </mapper>
         */
        Attribute attributeNamespace = rootElement.attribute("namespace");
        String namespace = attributeNamespace.getValue();

        /**
         * 解析(可能存在多个<select></select> 标签)
         * <select id="selectByid" resultType="com.tian.test.User" parameterType="java.lang.Integer">
         *         select * from user where id = #{id}
         * </select>
         * <select id="selectAll" resultType="com.tian.test.User" parameterType="java.lang.Integer">
         *         elect * from user where id = #{id}
         *</select>
         */
        List<Element> selectList = rootElement.selectNodes("//select");
        /**
         * 解析(可能存在多个<insert></insert> 标签)
         * <insert id="insert" resultType="java.lang.Integer" parameterType="com.tian.test.User">
         *         insert into user values (null,#{userName})
         * </insert>
         */
        List<Element> insertList = rootElement.selectNodes("//insert");

        //同理
        List<Element> updateList = rootElement.selectNodes("//update");
        List<Element> deleteList = rootElement.selectNodes("//delete");


        //把上面解析出来的所有element保存到一个大的list中
        List<Element> allList = new ArrayList<>();
        allList.addAll(selectList);
        allList.addAll(insertList);
        allList.addAll(updateList);
        allList.addAll(deleteList);

        for (Element element : allList) {
            /**
             * 获取 每条sql的 id属性值
             * <select id="selectById" ......></select>
             * <insert id="insert" ......></insert>
             * <update id="update" ......></update>
             * <delete id="delete" ......></delete>
             */
            String id = element.attributeValue("id");

            /**
             * <select id="selectById" resultType="com.tian.test.User" parameterType="java.lang.Integer">
             *         select * from user where id = #{id}
             * </select>
             */
            String resultType = element.attributeValue("resultType");
            String parameterType = element.attributeValue("parameterType");
            //除去空格后就是 sql 了
            String sql = element.getTextTrim();

            //然后把上面这些属性封装到我们的对象中
            MapperStatement mapperStatement = new MapperStatement();
            mapperStatement.setId(id);
            mapperStatement.setSql(sql);
            mapperStatement.setParameterType(parameterType);
            mapperStatement.setResultType(resultType);
            mapperStatement.setSqlType(element.getName());

            configuration.getMapperStatementMap().put(namespace + "." + id, mapperStatement);
        }
    }
}
